package com.baomidou.ant.schedule;

import com.baomidou.ant.entity.Notice;
import com.baomidou.ant.service.INoticeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

@Component
@EnableScheduling
@EnableAsync
public class NoticeScheduleTask {
    @Autowired
    private INoticeService noticeService;

    @Async
    @Scheduled(fixedDelay = 1000)
    public void updatePojectStatus() {
        //得到所有正在进行的公告
        List<Notice> noticeList=noticeService.querryByStatus("0");
        //获取当前时间
        LocalDateTime date=LocalDateTime.now();
        //对比当前时间
        for(Notice notice:noticeList){
            if((notice.getEndTime().compareTo(date))<0){
                notice.setStatus("1");
            }
        }
        if(noticeList.size()>0){
            //保存
            noticeService.saveOrUpdateBatch(noticeList);
        }
        System.out.println("定时任务进行中");
    }

}
